<template>
	<view id='submit'>
		<u-modal v-model="show" content="确定要删除吗？" @confirm="confirm"></u-modal>
		<view style="position: absolute;top:0;right: 0;">
			<text style="color: #DD524D;float: right;padding: 20rpx;font-size: 30rpx;font-weight: bold" @tap="manageAdress">{{tabText}}</text>
		</view>
		<view class="item" v-for="(res, index) in siteList" :key="res.id" style="border-bottom: 1px solid #eee">
			<view class="top">
				<view class="name">{{ res.name }}</view>
				<view class="phone">{{ res.phone }}</view>
				<view class="tag">
					<text v-for="(item, index) in res.tag" :key="index" :class="{red:item.tagText=='默认'}">{{ item.tagText }}</text>
				</view>
			</view>
			<view class="bottom">
				{{res.site}}
				<u-icon v-if="!showManage" @tap="toAddSite(res)" name="edit-pen" :size="40" color="#999999"></u-icon>
				<u-icon v-else @tap="delSite(res.id)" name="trash" :size="40" color="#DD524D"></u-icon>
			</view>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			siteList: [],
			showManage: false,
			tabText: '管理',
			show: false,
			localData: [],
			delId: undefined
		};
	},
	onLoad() {
		let that = this;
		uni.getStorage({
		    key: 'storage_city',
		    success: function (res) {
				that.localData = res.data
				console.log(res.data)
				if(res.data.city) {
					res.data.city.forEach((item, index)=>{
						if(item.tag==0) item.tag = '家'
						else if(item.tag==1) item.tag = '公司'
						else if(item.tag==2) item.tag = '学校'
						var tag =  [
							{
								tagText: item.tag
							}
						]
						
						let city = item.city+ "  "+item.detail;
						if(item.checked){
							tag =  [
								{
									tagText: '默认'
								},
								{
									tagText: item.tag
								}
							]
							
							that.siteList.unshift(
							{ id: item.id, name: item.name, phone: item.tel, tag: tag,
								site: city}
							)
						}else{
							that.siteList.push(
							{ id: item.id, name: item.name, phone: item.tel, tag: tag,
								site: city}
							)
						}
						
						
						
						
					})
					
				
				}
		    }
		});
		// this.getData();
	},
	methods: {
		//管理
		manageAdress(){
			this.showManage = !this.showManage;
			if(this.showManage) this.tabText = '取消'
			else  this.tabText = '管理'
		},
		//删除
		delSite(id) {
			let that = this;
			this.show = true;
			this.delId = id;
			
		},
		//确定删除
		confirm() {
			let that = this;
			this.show = false;
			console.log(this.localData.city)
			this.localData.city.forEach((item, index)=>{
				if(that.delId==item.id){
					that.localData.city.splice(index, 1)   
				}
			})
			uni.setStorage({
				key: 'storage_city',
				data: {
					city: this.localData.city,
					id: this.localData.id
				},
				success: function () {
					// console.log('success');
					that.siteList.forEach((item, index)=>{
						if(that.delId==item.id){
							that.siteList.splice(index, 1)   
						}
					})
				}
			});
			
		},
		getData() {
			
		},
		toAddSite(res){
			uni.navigateTo({
			    url: '/pages/index/submit/addSite?params='+JSON.stringify(res)
			});
		}
	}
};
</script>

<style lang="scss" scoped>
	#submit{
		padding: 30rpx;
		padding-top:60rpx;
		background-color: #f8f8f8;
	}
.item {
	background-color: #fff;
	padding: 40rpx 18rpx;
	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
